package edu.towson.cosc.cosc455.jirani2.compiler;

import java.util.ArrayList;
import java.util.Stack;
import java.util.TreeMap;

public class MySemanticAnalyzer {
	
	/** contains current stack to translate */
	public Stack<String> begStack = new Stack<String>();
	/** contains a translated version of the parseTree in html */
	public ArrayList<String> copyArray = new ArrayList<String>();
	/** TreeMap of variable names to coresponding values */
	public TreeMap<String, String> varMap = new TreeMap<String, String>();
	/** static string that will hold the entire translated parseTree in HTML */
	public static String semanticText = "";

	/**
	 * Method that will loop through parseTree, push current variables onto
	 * stack, and translate
	 */
	public void semantic(){
		for(int index = 0; index < MyCompiler.parseTree.size(); index++){
			//If begin tag push onto stack and translate
			if(LexicalTokens.begTags.contains(MyCompiler.parseTree.get(index).toUpperCase())){
				copyArray.add(translateBegin(MyCompiler.parseTree.get(index).toUpperCase()));
				begStack.push(translateBegin(MyCompiler.parseTree.get(index).toUpperCase()));
			}
			//If text push onto stack
			if(LexicalTokens.textDictionary.contains(MyCompiler.parseTree.get(index))){
				copyArray.add(MyCompiler.parseTree.get(index));
				begStack.push(MyCompiler.parseTree.get(index));
			}
			//IF end tag, pop, translate, and copy
			if(LexicalTokens.endTags.contains(MyCompiler.parseTree.get(index).toUpperCase())){
				String beg = begStack.pop();
				while(!LexicalTokens.beginHtmlTags.contains(beg)){
					beg = begStack.pop();
				}
				copyArray.add(translateEnd(beg));
			}
			//If var begin, put into map with value and continue to iterate
			if(LexicalTokens.VAR_BEGIN.equalsIgnoreCase(MyCompiler.parseTree.get(index))){
				index++;
				String varName = MyCompiler.parseTree.get(index);
				index++;
				index++;
				varMap.put(varName, MyCompiler.parseTree.get(index));
				index++;
			}
			//If var use, get varName from map, and print value
			if(LexicalTokens.VAR_USE.equalsIgnoreCase(MyCompiler.parseTree.get(index))){
				index++;
				if(!varMap.containsKey(MyCompiler.parseTree.get(index))){
					System.err.println("STATIC SEMANTIC ERROR! "+ 
				MyCompiler.parseTree.get(index) + " is not defined!");
					System.exit(0);
				}
				copyArray.add(MyCompiler.parseTree.get(index));
				begStack.push(MyCompiler.parseTree.get(index));
				index++;
			}
		}
		printSemantic();
	}
	
	/**
	 * print semantic will concat variable values and translated
	 * text into a string to be sent back to MyCompiler.java
	 */
	public void printSemantic(){
		for(int index = 0; index < copyArray.size(); index++){
			if(varMap.containsKey(copyArray.get(index))){
				semanticText += varMap.get(copyArray.get(index)) + "\n";
			}else{
				semanticText += copyArray.get(index) + " \n";
			}
		}
	}
	
	/**
	 * Returns HTML begin tag, given LOL begin
	 * @param lolToken
	 * @return html begin
	 */
	public String translateBegin(String lolToken){
		String htmlBeg = LexicalTokens.lolToHtml.get(lolToken);//Grabs html text from map
		return htmlBeg;
	}
	
	/**
	 * Returns html ending given an html begin
	 * @param htmlToken
	 * @return htmlEndToken
	 */
	public String translateEnd(String htmlToken){
		String htmlEnd = LexicalTokens.bteHtml.get(htmlToken);//Grabs from map
		return htmlEnd;
	}
	
	
}
